import json
import re


def cut_sentence(sentence):
    temp = ''
    result = []
    for char in sentence:
        if re.match(r'[0-9A-Za-z]', char):
            temp += char
        else:
            if len(temp) > 0:
                result.append(temp)
                temp = ''
            result.append(char)
    if len(temp) > 0:
        result.append(temp)
    return result


def find_position(inner, outer):
    for i in range(0, len(outer) - len(inner) + 1):
        j = 0
        res = []
        while j < len(inner) and inner[j] == outer[i + j]:
            res.append(i + j)
            j += 1
        if len(res) == len(inner):
            return res


def convert_data(file_name):
    data = json.load(open(file_name))
    results = []
    reserve_word = {'是','否','none',''}
    empty_word = {'none', ''}
    for obj in data:
        turns = filter(lambda x: x['role'] == 'usr', obj['turns'])
        for turn in turns:
            result = {}
            result['utterance'] = cut_sentence(turn['utterance'])
            intent_set = set()
            slots_dict = {}
            dialog_act = turn['dialog_act']
            for acts in dialog_act:
                intent = acts[0] + '+' + acts[1]
                if acts[3] in reserve_word:
                    if acts[2] not in empty_word:
                        intent += '+' + acts[2]
                    if acts[3] not in empty_word:
                        intent += '+' + acts[3]
                else:
                    slot_cut = cut_sentence(acts[3])
                    position = find_position(slot_cut, result['utterance'])
                    if position:
                        slots_dict[intent + '+' + acts[2]] = position
                intent_set.add(intent)
            result['intents'] = list(intent_set)
            slots = ['O' for _ in result['utterance']]
            for key in slots_dict:
                for j, pos in enumerate(slots_dict[key]):
                    if j == 0:
                        slots[pos] = 'B+' + key
                    else:
                        slots[pos] = 'I+' + key
            result['slots'] = slots
            results.append(result)
    return results


if __name__ == '__main__':
    for key in ['train', 'val', 'test']:
        result = convert_data('readable_data/readable_' + key + '_data.json')
        json.dump(result, open('readable_data/joint_' + key + '_data.json', 'w'), ensure_ascii=False)

